<template>
  <div class="user_container">
    <my-bread :list="['用户管理', '用户列表']"></my-bread>
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item></el-breadcrumb-item>
      <el-breadcrumb-item></el-breadcrumb-item>
    </el-breadcrumb> -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入搜索内容" v-model="pageData.query">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchUser()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="18">
          <el-button
            type="primary"
            @click="'dialogVisible = !dialogVisible', resetUserForm()"
            >添加用户</el-button
          >
          <el-dialog title="添加用户" :visible.sync="dialogVisible">
            <el-form
              ref="addUserForm"
              :model="form"
              label-width="80px"
              :rules="rules"
            >
              <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="form.password"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="form.email"></el-input>
              </el-form-item>
              <el-form-item label="手机号" prop="mobile">
                <el-input v-model="form.mobile"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="addUserSubmit()"
                >确 定</el-button
              >
            </span>
          </el-dialog>
        </el-col>
      </el-row>
      <el-table :data="usersList" border style="width: 100%">
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="mobile" label="手机号"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <!-- scope 是组件传给使用组件的页面的作用域数据  数据中row代表当前行数据 -->
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeState(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="openeditDialog(scope.row)"
              ></el-button>
              <el-button
                type="primary"
                icon="el-icon-delete"
                @click="delUser(scope.row.id)"
              ></el-button>
              <el-button
                type="primary"
                icon="el-icon-setting"
                @click="getRoleList(scope.row)"
              ></el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :page-count="total"
        :current-page="pageData.pagenum"
        @current-change="pageChange"
      ></el-pagination>
    </el-card>
    <!-- 分配角色对话框 -->
    <el-dialog title="分配角色" :visible.sync="roleDialogFormVisible">
      <el-form :model="form">
        <el-form-item label="当前用户：">{{ roleUsername }}</el-form-item>
        <el-form-item label="当前用户角色：">{{ role_name }}</el-form-item>
        <el-form-item label="分配角色">
          <el-select placeholder="请选择角色" v-model="roleSelected">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="roleDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeRole()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogFormVisible">
      <el-form
        :model="editForm"
        :rules="editRules"
        autocomplete="off"
        ref="editForm"
      >
        <el-form-item label="用户名">
          <el-input disabled v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import user_mixin from "./user_mixin";
export default {
  mixins: [user_mixin]
};
</script>
<style lang="less">
.box-card {
  margin-top: 20px;
  .el-table {
    margin: 20px 0;
  }
  .el-pagination {
    float: right;
    margin-bottom: 20px;
  }
}
</style>
